package org.itboys.weixin.controller;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.weixin.entity.WeixinConfig;
import org.itboys.weixin.service.WeixinConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 包名：com.hz.sunday.xlcl.controller.web
 * 作者 : 江雄
 * Email: empty90911@126.com
 * 时间：2015年10月8日 下午3:47:21
 * 描述:圣代_微信配置_controller_web
 *
 */

@Controller
@RequestMapping("/sunday/web/weixin/config")
public class SundayWeiXinConfigController extends BaseController {

    @Autowired(required = false)
    private WeixinConfigService weixinConfigService;
    /**
     * 微信配置主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            Model model){
        return "/sunday/weixin/config/index";
    }

    /**
     * 查询当个配置
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/findOne")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="id",required=true)Long id){
        try {
            WeixinConfig config = weixinConfigService.getById(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgResultDO(config, CommonConstants.SUCCESS));
        } catch (Exception e) {
            logger.error("/sunday/weixin/config/findOne------"+e.getMessage());
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,  CommonConstants.ERROMSG));
        }
    }

    /**
     * 新增或编辑微信配置
     * @param response
     * @param weixinConfig
     */
    @RequestMapping("/save")
    public void save(HttpServletResponse response,
                     @ModelAttribute WeixinConfig weixinConfig) {
        try {
            if (weixinConfig.getId() == 0L) {

                weixinConfigService.save(weixinConfig);
            } else {
                weixinConfigService.update(weixinConfig);
            }
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getErrorResultDO(CommonConstants.SUCCESS_CODE,  CommonConstants.ERROMSG));
        } catch (Exception e) {
            logger.error("/sunday/weixin/config/save------"+e.getMessage());
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,  CommonConstants.ERROMSG));
        }
    }

    /**
     * 分页查询微信配置。不分页
     * @param response
     * @param request
     */
    @RequestMapping("/select")
    public void list(HttpServletResponse response, HttpServletRequest request) {
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted",0);
        List<WeixinConfig> configs=weixinConfigService.list(param);
        JsonPageUtils.renderJsonPage(configs != null && configs.size() > 0 ? configs.size() : 0L, configs, response);

    }


}
